package com.wenhai.redis.lettuce;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.SetArgs;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import org.assertj.core.api.Assertions;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class LettuceSyncDemo {
    public static void main(String[] args) {
        RedisURI redisURI = RedisURI.builder()
                .withPassword("admin")
                .withHost("192.168.152.138")
                .withPort(6379)
                .withTimeout(Duration.of(10,ChronoUnit.SECONDS))
                .build();
        RedisClient redisClient = RedisClient.create(redisURI);
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        RedisCommands<String, String> redisCommands = connect.sync();
        SetArgs setArgs = SetArgs.Builder.nx().ex(5);
        String result = redisCommands.set("name", "wenhai", setArgs);
        Assertions.assertThat(result).isEqualToIgnoringCase("OK");
        result = redisCommands.get("name");
        Assertions.assertThat(result).isEqualToIgnoringCase("wenhai");
        connect.close();
        redisClient.shutdown();

    }
}
